/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.FavoritesDao;
import com.qdu.entity.Book;
import com.qdu.entity.Favorites;
import com.qdu.entity.Movie;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ice
 */
public class FavoritesDaoImpl extends BaseDaoImpl1 implements FavoritesDao {

    @Override
    public int addFavorites(Favorites f) {
        return executeUpdate("insert into Favorites values(?,?,?)", f.getId(), f.getMBID(), f.getTime());
    }

    @Override
    public int deleteFavorites(Favorites f) {
        return executeUpdate("delete Favorites where id=? and MBID=?", f.getId(), f.getMBID());
    }

    @Override
    public List<Movie> getMovieList(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select MovieId,MCover,MChineseName,Mdirector from Favorites,Movie where id=? and MBID=MovieId");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Movie(rs.getString(1), rs.getString(2), rs.getString(3),rs.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public List<Book> getBookList(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Book> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select BID,BCover,BName,Bpublisher from Favorites,Book where id=? and MBID=BID");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Book(rs.getString(1), rs.getString(2), rs.getString(3),rs.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }



}
